package com.meituan.catering.management.order.biz.model.converter;

import com.meituan.catering.management.common.model.biz.PageBO;
import com.meituan.catering.management.order.biz.model.CateringOrderBO;
import com.meituan.catering.management.order.dao.model.CateringOrderDO;
import com.meituan.catering.management.order.dao.model.request.SearchCateringOrderDataRequest;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Believe
 * Date 2022/5/27 14:39
 */
public class PageBOConverter {
    public static final Integer DEFAULT_PAGE_SIZE=10;

    public static PageBO<CateringOrderBO> toPageBO(List<CateringOrderDO> orderDOList, Integer countAll, SearchCateringOrderDataRequest searchCateringOrderDataRequest){
        PageBO<CateringOrderBO> pageBO = new PageBO<>();
        pageBO.setPageIndex(searchCateringOrderDataRequest.getPageIndex());
        pageBO.setPageSize(searchCateringOrderDataRequest.getPageSize());
        pageBO.setTotalPageCount(totalPageCount(countAll));
        pageBO.setTotalCount(countAll);
        pageBO.setRecords(toCateringOrderBOList(orderDOList));
        return pageBO;
    }

    public static List<CateringOrderBO> toCateringOrderBOList(List<CateringOrderDO> orderDOList){
        List<CateringOrderBO> cateringOrderBOList = new LinkedList<>();
        for (CateringOrderDO cateringOrderDO : orderDOList) {
            cateringOrderBOList.add(CateringOrderBOConverter.toCateringOrderBO(cateringOrderDO));
        }
        return cateringOrderBOList;
    }

    public static Integer totalPageCount(Integer countAll){
        if (countAll % DEFAULT_PAGE_SIZE == 0){
            return countAll / DEFAULT_PAGE_SIZE;
        }
        return countAll / DEFAULT_PAGE_SIZE + 1;
    }
}
